package resp

import (
	"github.com/gin-gonic/gin"
	"go_blok/server/utils/custbinding"
)

type ResponseJson struct {
	Code int         `json:"code"`
	Msg  interface{} `json:"msg"`
	Data interface{} `json:"data"`
}

func response(code int, msg interface{}, data interface{}, c *gin.Context) {
	resp := ResponseJson{
		Code: code,
		Msg:  msg,
		Data: data,
	}
	c.JSON(200, resp)
}

func OkWithData(data interface{}, c *gin.Context) {
	response(200, "成功", data, c)
}

func OkWithMsg(msg string, c *gin.Context) {
	response(200, msg, nil, c)
}

func Ok(msg string, data interface{}, c *gin.Context) {
	response(200, msg, data, c)
}

func Fail(code int, msg string, c *gin.Context) {
	response(1001, msg, nil, c)
}

func FailWithMsg(msg string, c *gin.Context) {
	response(1001, msg, nil, c)
}

func FailWithError(err error, c *gin.Context) {
	response(1001, err.Error(), nil, c)
}

func FailWithBind(err error, c *gin.Context) {
	resp := custbinding.ValidateError(err)
	response(1001, resp.Msg, resp.FieldMap, c)
}
